package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.CTrucksDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.TruckExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TruckListVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TrucksVo;
import com.xiaoxu.intranetweb.util.IllegalCharacterCheck;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Future;

@Component
public class TruckBu {
    @Autowired
    private ICTrucksService iCTrucksService;
    @Autowired
    private ICTruckClassService icTruckClassService;
    @Autowired
    private ITTruckBlacklistService truckBlacklistService;
    @Autowired
    private ICPdtsAuthService icPdtsAuthService;
    @Autowired
    private ICCompanysService iCCompanysService;
    @Autowired
    private ICProductsInfoService iCProductsInfoService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private CTrucksDic cTrucksDic;
    @Autowired
    private ICIcCardsInfoService iCIcCardsInfoService;
    @Autowired
    private ICSysCfgService iCSysCfgService;

    /**
     * 获取车辆列表
     *
     * @param page      页码
     * @param pageSize  每页条数
     * @param truckNo   车牌号
     * @param truckType 车辆类型
     * @return SzResult
     */
    public SzResult getTruck(String page, String pageSize, String truckNo, String truckType,String bindCoId,  String token, String plantId) throws Exception {
//        String plantId = null;
//        if (token != null) {
//            plantId = plantId;
//        }
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(truckNo != null && !truckNo.equals(""),i->{
            i.like("TRUCKS_TRUCKNO", truckNo).or().like("BACKGROUND_CO",truckNo);
        });
        queryWrapper.apply(StringUtils.isNotEmpty(bindCoId),"FIND_IN_SET('" + bindCoId + "', BIND_CO_ID)");
        queryWrapper.eq(truckType != null, "TRUCKS_TYPE", truckType);
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.eq(plantId != null && !plantId.equals(""), "DATA_FROM", plantId);
        //queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
        queryWrapper.orderByDesc("ID");
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        List<TrucksVo> trucksVos = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TrucksVo trucksVo = new TrucksVo();
            BeanUtil.copyProperties(cTrucks,trucksVo);
            if (cTrucks.getTrucksCheckinCo() == null || cTrucks.getTrucksCheckinCo().equals("")) {
                trucksVo.setTrucksCheckinCoName("");
            } else {
                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
                queryWrapper1.eq("CPY_DELETED", 0);
                queryWrapper1.eq("DATA_FROM", plantId);
                CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
                if (cCompanys == null)
                    trucksVo.setTrucksCheckinCoName("");
                else
                    trucksVo.setTrucksCheckinCoName(cCompanys.getCpyCo());
            }
            trucksVo.setTrucksCheckinTag(cTrucks.getTrucksCheckinTag());
            trucksVo.setTrucksCheckinPdt(cTrucks.getTrucksCheckinPdt());
            if (cTrucks.getTrucksCheckinPdt() == null || cTrucks.getTrucksCheckinPdt().equals("")) {
                trucksVo.setTrucksCheckinPdtName("");
            } else {
                QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cTrucks.getTrucksCheckinPdt());
                queryWrapper2.eq("PDTINF_DELETED", 0);
                queryWrapper2.eq("DATA_FROM", plantId);
                CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
                if (cProductsInfo == null)
                    trucksVo.setTrucksCheckinPdtName("");
                else
                    trucksVo.setTrucksCheckinPdtName(cProductsInfo.getPdtinfPdt());
            }
            trucksVo.setTrucksCheckinPdtsn(cTrucks.getTrucksCheckinPdtsn());
            trucksVo.setTrucksCheckinCoTran(cTrucks.getTrucksCheckinCoTran());
            if (cTrucks.getTrucksCheckinCoTran() == null || cTrucks.getTrucksCheckinCoTran().equals("")) {
                trucksVo.setTrucksCheckinCoTranName("");
            } else {
                QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cTrucks.getTrucksCheckinCoTran());
                queryWrapper3.eq("CPY_DELETED", 0);
                queryWrapper3.eq("DATA_FROM", plantId);
                CCompanys cCompanys1 = iCCompanysService.getOne(queryWrapper3);
                if (cCompanys1 == null) {
                    trucksVo.setTrucksCheckinCoTranName("");
                } else {
                    trucksVo.setTrucksCheckinCoTranName(cCompanys1.getCpyCo());
                }
            }
            QueryWrapper<TTruckBlacklist> truckBlacklistQueryWrapper = new QueryWrapper<>();
            truckBlacklistQueryWrapper.ge("END_TIME", LocalDateTime.now());
//            TTruckBlacklist truckBlacklist = truckBlacklistService.getOne(truckBlacklistQueryWrapper);
            List<TTruckBlacklist> truckBlacklist = truckBlacklistService.list(truckBlacklistQueryWrapper);
            if (truckBlacklist.size() >0){
                trucksVo.setIsBlacklist(1);
            }else {
                trucksVo.setIsBlacklist(0);
            }
            if (StringUtils.isNotEmpty(cTrucks.getBindCoId())){
                List<Map<String,Object>> coList = new ArrayList<>();
                for (String id : Arrays.asList(cTrucks.getBindCoId().split(","))) {
                    QueryWrapper<CCompanys> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("ID", id);
                    queryWrapper4.eq("CPY_DELETED", 0);
                    queryWrapper4.eq("DATA_FROM", plantId);
                    for (CCompanys cCompanys : iCCompanysService.list(queryWrapper4)) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("coName",cCompanys.getCpyShrtname());
                        map.put("id",cCompanys.getId());
                        coList.add(map);
                    }
                }
                trucksVo.setCoList(coList);
            }
            trucksVo.setTruckQueueVipNums(cTrucks.getTrucksQueueVipNums());
            trucksVos.add(trucksVo);
        }
        Long total = iPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", trucksVos);
        return SzResult.success(map);
    }
//    查询车辆列表
    public SzResult getTruckList(String page, String pageSize, String truckNo, String bindCoId, String token, String plantId) {
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.apply(bindCoId != null && !bindCoId.equals(""),"FIND_IN_SET('" + bindCoId + "', BIND_CO_ID)");
//        queryWrapper.eq("TRUCKS_QUEUE_STATUS",0);
        queryWrapper.and(i -> i.eq("TRUCKS_QUEUE_STATUS",0).or().isNull("TRUCKS_QUEUE_STATUS"));
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq(plantId != null && !plantId.equals(""), "DATA_FROM", plantId);
        queryWrapper.orderByDesc("TRUCKS_ID");
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        List<TruckListVo> truckList = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TruckListVo truckListVo = new TruckListVo();
            BeanUtils.copyProperties(cTrucks,truckListVo);
            if (cTrucks.getTrucksType() != null){
                QueryWrapper<CTruckClass> truckClassQw= new QueryWrapper<>();
                truckClassQw.eq("TRUCKCLASS_ID",cTrucks.getTrucksType());
                truckListVo.setTrucksTypeName(icTruckClassService.getOne(truckClassQw).getTruckclassClass());
            }
            if (cTrucks.getBindCoId() != null && !cTrucks.getBindCoId().equals("")){
                List<Map<String,Object>> companyList = new ArrayList<>();
//                拿到车辆的所有的绑定的单位信息
                for (String s : cTrucks.getBindCoId().split(",")) {
                    CCompanys companys = iCCompanysService.getById(s);
                    Map<String, Object> map = new HashMap<>();
                    map.put("coName",companys.getCpyShrtname());
                    map.put("id",companys.getId());
                    companyList.add(map);
                }
                truckListVo.setCompanyList(companyList);
            }
            truckList.add(truckListVo);
        }
        Map<String, Object> res = new HashMap<>();
        res.put("total", iPage.getTotal());
        res.put("list", truckList);
        return SzResult.success(res);
    }
    public SzResult getTruckQueueInfo (String coId){
        Map<String, Object> res = new HashMap<>();
       QueryWrapper<CCompanys> cCompanysQw = new QueryWrapper<>();
       cCompanysQw.eq("ID", coId);
       CCompanys cCompanys = iCCompanysService.getOne(cCompanysQw);
        List<Map<String,Object>> pdtList = new ArrayList<>();
        QueryWrapper<CPdtsAuth> cPdtsAuthQw = new QueryWrapper<>();
        cPdtsAuthQw.eq("PDTSAUTH_CO",cCompanys.getCpyCo());
        cPdtsAuthQw.eq("PDTSAUTH_CHICO", cCompanys.getCpyChico());
        cPdtsAuthQw.eq("DATA_FROM", cCompanys.getDataFrom());
        cPdtsAuthQw.eq("PDTSAUTH_DELETED",0);
        for (CPdtsAuth cPdtsAuth : icPdtsAuthService.list(cPdtsAuthQw)) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", cPdtsAuth.getId());
            map.put("pdtName", cPdtsAuth.getPdtsauthPdt());
            QueryWrapper<CProductsInfo> cProductsInfoQW = new QueryWrapper<>();
            cProductsInfoQW.eq("PDTINF_PDT",cPdtsAuth.getPdtsauthPdt()).select("PDTINF_SPE_SITELIST");
            cProductsInfoQW.eq("DATA_FROM", cPdtsAuth.getDataFrom());
            cProductsInfoQW.eq("PDTINF_DELETED", 0);
            CProductsInfo ProductsInfo = iCProductsInfoService.getOne(cProductsInfoQW);
            if (ProductsInfo != null) {
//                System.out.println(ProductsInfo.getPdtinfSpeSitelist());
                map.put("sitelistList", ProductsInfo.getPdtinfSpeSitelist().split(","));
            }else {
                map.put("sitelistList", new ArrayList<>());
            }
            pdtList.add(map);
        }
        res.put("pdtList", pdtList);
        return SzResult.success(res);
    }
    //新增车辆
    public SzResult addTruck(String trucksTruckno, String trucksTrailerno, String trucksType, Integer trucksLength, Integer trucksEmiss, Integer trucksAxles, Integer trucksRefTare, Integer trucksRefNet,
                             String trucksMaster, String trucksIdentity, String trucksTell,String bindCoId,String backgroundCo,Integer ifBackgrounded, String token, String plantId) throws Exception {
        if (IllegalCharacterCheck.isNameLawful(trucksTruckno)){
            return SzResult.error("车号里有非法字符");
        }
        // String plantId = plantId;
        CTrucks cTrucks = new CTrucks();
        if (trucksTruckno == null || trucksTruckno.isEmpty()) {
            return SzResult.error("车牌号不能为空");
        }
        if (trucksType == null) {
            return SzResult.error("车辆类型不能为空");
        }
//如果trucksRefTare为Null或者为""或者为0那么trucksRefTare就是一个默认值
        if (trucksRefTare == null || trucksRefTare == 0) {
            QueryWrapper<CSysCfg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("SYSCFG_ITEM_NAME", "TempTruckDefRefTare");
            queryWrapper.eq("DATA_FROM", plantId);
            CSysCfg one = iCSysCfgService.getOne(queryWrapper);
            BigDecimal syscfgLongValue = one.getSyscfgLongValue();
            cTrucks.setTrucksRefTare(syscfgLongValue);
        } else {
            cTrucks.setTrucksRefTare(BigDecimal.valueOf(trucksRefTare));
        }
        if (trucksRefNet == null || trucksRefNet == 0) {
            QueryWrapper<CSysCfg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("SYSCFG_ITEM_NAME", "TempTruckDefRefNet");
            queryWrapper.eq("DATA_FROM", plantId);
            CSysCfg one = iCSysCfgService.getOne(queryWrapper);
            BigDecimal syscfgLongValue = one.getSyscfgLongValue();
            cTrucks.setTrucksRefNet(syscfgLongValue);
        } else {
            cTrucks.setTrucksRefNet(BigDecimal.valueOf(trucksRefNet));
        }


        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_TRUCKNO", trucksTruckno);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        List<CTrucks> list = iCTrucksService.list(queryWrapper);
        if (list.size()>0) {
            return SzResult.error("车牌号已存在,请重新输入");
        }

//        QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("TRUCKS_TRAILERNO", trucksTrailerno);
//        queryWrapper1.eq("TRUCKS_DELETED", 0);
//        queryWrapper1.eq("DATA_FROM", plantId);
//        queryWrapper1.last("limit 1");
//        CTrucks one1 = iCTrucksService.getOne(queryWrapper1);
//        if (one1 != null) {
//            return SzResult.error("挂车号已存在,请重新输入");
//        }


        //trucksTruckno是由英文和数字组成
        if (!trucksTruckno.matches("^[\\u4e00-\\u9fa5A-Za-z0-9]+$")) {
            return SzResult.error("车牌号只能由中文,英文和数字组成");
        }
//        if (trucksTruckno.length() != 7) {
//            return SzResult.error("车牌号只能是7位");
//        }


        cTrucks.setTrucksTruckno(trucksTruckno);
        if (trucksTrailerno != null && !trucksTrailerno.isEmpty()) {
            cTrucks.setTrucksTrailerno(trucksTrailerno);
        }
        cTrucks.setTrucksType(BigDecimal.valueOf(Long.parseLong(trucksType)));

//        cTrucks.setTrucksType(trucksType);
        if (trucksLength != null) {
            cTrucks.setTrucksLength(BigDecimal.valueOf(trucksLength));
        }
        if (trucksEmiss != null) {
            cTrucks.setTrucksEmiss(BigDecimal.valueOf(trucksEmiss));
        }
        if (trucksAxles != null) {
            cTrucks.setTrucksAxles(BigDecimal.valueOf(trucksAxles));
        }

        if (trucksMaster != null && !trucksMaster.isEmpty()) {
            cTrucks.setTrucksMaster(trucksMaster);
        }
        if (trucksIdentity != null && !trucksIdentity.isEmpty()) {
            cTrucks.setTrucksIdentity(trucksIdentity);
        }
        if (trucksTell != null && !trucksTell.isEmpty()) {
            cTrucks.setTrucksTell(trucksTell);
        }
        if (bindCoId != null && !bindCoId.equals("")) {
            cTrucks.setBindCoId(bindCoId);
        }
        if (StringUtils.isNotEmpty(backgroundCo)){
            cTrucks.setBackgroundCo(backgroundCo);
        }
        if (ifBackgrounded != null){
            cTrucks.setIfBackgrounded(ifBackgrounded);
        }
        cTrucks.setTrucksDeleted(0);
        cTrucks.setTrucksInfoApproved(1);
        //形成时间戳
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        format = format.replace("-", "").replace(" ", "").replace(":", "");
        cTrucks.setId(format);
//        cTrucks.setTrucksId(format);
        // 创建一个新的QueryWrapper实例
        QueryWrapper<CTrucks> maxIdQueryWrapper = new QueryWrapper<>();

// 按照TRUCKS_ID字段降序排序
        maxIdQueryWrapper.orderByDesc("TRUCKS_ID");

// 限制查询结果为1条记录
        maxIdQueryWrapper.last("limit 1");

// 执行查询，获取最大的一条记录
        CTrucks trucks = iCTrucksService.getOne(maxIdQueryWrapper);

// 如果找到了记录，则基于最大ID生成新的ID；否则设置为1
        if (trucks != null) {
            BigDecimal trucksId = trucks.getTrucksId();
            // 将当前最大ID加1作为新ID
            cTrucks.setTrucksId(trucksId.add(BigDecimal.ONE));
        } else {
            // 如果没有找到任何记录，设置初始ID为1
            cTrucks.setTrucksId(new BigDecimal(1));
        }
        cTrucks.setTrucksEntered(0);//0为不允许,1为允许
        SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        cTrucks.setTrucksRegMan(user1.getTrueName());
        //判断标签1，标签2，标签3是否存在，有一个存在即可
        if (cTrucks.getDownedID16() != null || cTrucks.getDownedID17() != null || cTrucks.getDownedID18() != null
                || cTrucks.getDownedID19() != null || cTrucks.getDownedID20() != null) {
            cTrucks.setTrucksFixMan(user1.getTrueName());
        }
        cTrucks.setDataFrom(plantId);
        cTrucks.setTrucksRefTareInited(0);
        cTrucks.setTrucksMustQueue(0);
        cTrucks.setTrucksIdentyQueue(0);
        cTrucks.setTrucksIdentyLoad(0);
        cTrucks.setTrucksInfoApproved(1);
        cTrucks.setDownedID1(0);
        cTrucks.setDataFrom(plantId);

//        TRUCKS_MUST_QUEUE
//从配置文件里拿到默认是否必须排队
            QueryWrapper<CSysCfg> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("SYSCFG_ITEM_NAME", "TempTruckDefMustQueue");
            queryWrapper2.eq("DATA_FROM", plantId);
            CSysCfg one2 = iCSysCfgService.getOne(queryWrapper2);
            if (one2 != null) {
                BigDecimal syscfgLongValue = one2.getSyscfgLongValue();
                cTrucks.setTrucksMustQueue(Integer.valueOf(String.valueOf(syscfgLongValue)));
            }else{
                cTrucks.setTrucksMustQueue(0);
            }
//        TRUCKS_AGAIN_MIN_TM新加车辆再次排队进厂离最后一次重车出厂的最短时间
            QueryWrapper<CSysCfg> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("SYSCFG_ITEM_NAME", "TempTruckDefAgainMinTm");
            queryWrapper3.eq("DATA_FROM", plantId);
            CSysCfg one3 = iCSysCfgService.getOne(queryWrapper3);
            if (one3 != null) {
                BigDecimal syscfgLongValue = one3.getSyscfgLongValue();
                cTrucks.setTrucksAgainMinTm(syscfgLongValue);
            }else{
                cTrucks.setTrucksAgainMinTm(BigDecimal.valueOf(0));
            }
//            TRUCKS_IDENTY_QUEUE新加车辆默认默认识别排队
            QueryWrapper<CSysCfg> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("SYSCFG_ITEM_NAME", "TempTruckDefIdentyQueue");
            queryWrapper4.eq("DATA_FROM", plantId);
            CSysCfg one4 = iCSysCfgService.getOne(queryWrapper4);
            if (one4 != null) {
                BigDecimal syscfgLongValue = one4.getSyscfgLongValue();
                cTrucks.setTrucksIdentyQueue(Integer.valueOf(String.valueOf(syscfgLongValue)));
            }else{
                cTrucks.setTrucksIdentyQueue(0);
            }
//            TRUCKS_IDENTY_LOAD新加车辆默认识别装载
            QueryWrapper<CSysCfg> queryWrapper5 = new QueryWrapper<>();
            queryWrapper5.eq("SYSCFG_ITEM_NAME", "TempTruckDefIdentyLoad");
            queryWrapper5.eq("DATA_FROM", plantId);
            CSysCfg one5 = iCSysCfgService.getOne(queryWrapper5);
            if (one5 != null) {
                BigDecimal syscfgLongValue = one5.getSyscfgLongValue();
                cTrucks.setTrucksIdentyLoad(Integer.valueOf(String.valueOf(syscfgLongValue)));
            }else{
                cTrucks.setTrucksIdentyLoad(0);
            }
//            TRUCKS_INFO_APPROVED新加车辆默认被批准
            QueryWrapper<CSysCfg> queryWrapper6 = new QueryWrapper<>();
            queryWrapper6.eq("SYSCFG_ITEM_NAME", "TempTruckDefApprved");
            queryWrapper6.eq("DATA_FROM", plantId);
            CSysCfg one6 = iCSysCfgService.getOne(queryWrapper6);
            if (one6 != null) {
                BigDecimal syscfgLongValue = one6.getSyscfgLongValue();
                cTrucks.setTrucksInfoApproved(Integer.valueOf(String.valueOf(syscfgLongValue)));
            }else{
                cTrucks.setTrucksInfoApproved(1);
            }
//            TRUCKS_LENGTH新加车辆（非手动登记的）默认的车长（米）
            QueryWrapper<CSysCfg> queryWrapper7 = new QueryWrapper<>();
            queryWrapper7.eq("SYSCFG_ITEM_NAME", "TempTruckDefLength");
            queryWrapper7.eq("DATA_FROM", plantId);
            CSysCfg one7 = iCSysCfgService.getOne(queryWrapper7);
            if (one7 != null) {
                BigDecimal syscfgLongValue = one7.getSyscfgLongValue();
                cTrucks.setTrucksLength(syscfgLongValue);
            }
        iCTrucksService.save(cTrucks);
        sysLogBu.logRecord("新增车辆信息", user1.getTrueName(), "新增了车牌号为" + trucksTruckno + "的车辆信息", plantId);
        return SzResult.Success();
    }

    //删除车辆
    public SzResult deleteTruck(String trucksTruckno, String token, String plantId) throws Exception {
        //   String plantId = plantId;
        QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("TRUCKS_TRUCKNO", trucksTruckno);
        queryWrapper1.between("TRUCKS_QUEUE_STATUS", 1, 6);
        queryWrapper1.eq("TRUCKS_DELETED", 0);
        queryWrapper1.eq("DATA_FROM", plantId);
        CTrucks one1 = iCTrucksService.getOne(queryWrapper1);
        if (one1 != null) {
            return SzResult.error("厂内有车辆运行，请等车辆离厂后再操作");
        }

        if (trucksTruckno == null || trucksTruckno.equals("")) {
            return SzResult.error("车牌号不能为空");
        }
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_TRUCKNO", trucksTruckno);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks one = iCTrucksService.getOne(queryWrapper);
        one.setTrucksDeleted(1);
        one.setDownedID1(0);
        iCTrucksService.updateById(one);
        return SzResult.Success();
    }

//    public SzResult updateBgTruck(String id, String backgroundCo, Integer ifBackgrounded) {
//        CTrucks trucks = new CTrucks();
//        trucks.setId(id);
//        trucks.setBackgroundCo(backgroundCo);
//        trucks.setIfBackgrounded(ifBackgrounded);
//        if (iCTrucksService.updateById(trucks)) {
//            return SzResult.success("修改成功");
//        }
//        return SzResult.error("修改失败");
//    }

    //修改车辆
    public SzResult updateTruck(String id, String trucksTruckno, String trucksTrailerno, String trucksType, Integer trucksLength, Integer trucksEmiss, Integer trucksAxles, Integer trucksRefTare, Integer trucksRefNet,
                                String trucksMaster, String trucksIdentity, String trucksTell, String trucksAgainMinTm,String bindCoId,String backgroundCo,Integer ifBackgrounded, String token, String plantId) throws Exception {
        if (id != null && !id.isEmpty()) {
            QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ID", id);
            queryWrapper.eq("TRUCKS_DELETED", 0);
            CTrucks one = iCTrucksService.getOne(queryWrapper);
            if (one != null) {
                if (trucksTrailerno != null && !trucksTrailerno.isEmpty()) {
                    //校验除了自己的挂车号，其他的挂车号是否重复
//                    QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("TRUCKS_TRAILERNO", trucksTrailerno);
//                    queryWrapper1.eq("TRUCKS_DELETED", 0);
//                    queryWrapper1.ne("TRUCKS_ID", one.getTrucksId());
//                    queryWrapper1.eq("DATA_FROM", plantId);
//                    queryWrapper1.last("limit 1");
//                    CTrucks one1 = iCTrucksService.getOne(queryWrapper1);
//                    if (one1 != null) {
//                        return SzResult.Error("挂车号已存在,请重新输入");
//                    }
                    one.setTrucksTrailerno(trucksTrailerno);
                }else{
                    one.setTrucksTrailerno("");
                }
                if (trucksType != null) {
                    one.setTrucksType(BigDecimal.valueOf(Long.parseLong(trucksType)));
                }
                if (trucksLength != null) {
                    if (trucksLength < 0) {
                        return SzResult.error("车长不能为负数");
                    }
                    one.setTrucksLength(BigDecimal.valueOf(trucksLength));
                }
                if (trucksEmiss != null) {

                    one.setTrucksEmiss(BigDecimal.valueOf(trucksEmiss));
                }else {
                    one.setTrucksEmiss(BigDecimal.valueOf(0));
                }
                if (trucksAxles != null) {
                    if (trucksAxles < 0) {
                        return SzResult.error("轴数不能为负数");
                    }
                    one.setTrucksAxles(BigDecimal.valueOf(trucksAxles));
                }
                if (trucksRefTare != null) {
                    if (trucksRefTare < 0) {
                        return SzResult.error("皮重不能为负数");
                    }
                    one.setTrucksRefTare(BigDecimal.valueOf(trucksRefTare));
                }
                if (trucksRefNet != null) {
                    if (trucksRefNet < 0) {
                        return SzResult.error("净重不能为负数");
                    }
                    one.setTrucksRefNet(BigDecimal.valueOf(trucksRefNet));
                }
                if (trucksMaster != null && !trucksMaster.equals("")) {
                    one.setTrucksMaster(trucksMaster);
                }else {
                    one.setTrucksMaster("");
                }
                if (trucksIdentity != null && !trucksIdentity.equals("")) {
                    one.setTrucksIdentity(trucksIdentity);
                }else {
                    one.setTrucksIdentity("");
                }
                if (trucksTell != null && !trucksTell.equals("")) {
                    one.setTrucksTell(trucksTell);
                }else {
                    one.setTrucksTell("");
                }
                if (trucksAgainMinTm != null && !trucksAgainMinTm.equals("")) {
                    one.setTrucksAgainMinTm(BigDecimal.valueOf(Long.parseLong(trucksAgainMinTm)));
                }else {
                    one.setTrucksAgainMinTm(BigDecimal.valueOf(0));
                }
                if (StringUtils.isNotEmpty(backgroundCo)){
                    one.setBackgroundCo(backgroundCo);
                }else {
                    one.setBackgroundCo("");
                }
                if (ifBackgrounded != null){
                    one.setIfBackgrounded(ifBackgrounded);
                }
                    one.setBindCoId(bindCoId);
                SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                String trucksTruckno1 = one.getTrucksTruckno();
                if (trucksTruckno == null || trucksTruckno.equals("")) {
                    sysLogBu.logRecord("修改车辆信息", user1.getTrueName(), "修改了车牌号为" + trucksTruckno1 + "的车辆信息", plantId);
                } else {
//                    QueryWrapper<CTrucks> queryWrapper2 = new QueryWrapper<>();
//                    queryWrapper2.eq("TRUCKS_TRUCKNO", trucksTruckno);
//                    queryWrapper2.ne("TRUCKS_DELETED", 1);
//                    queryWrapper2.ne("ID", id);
//                    List<CTrucks> one1 = iCTrucksService.list(queryWrapper2);
//                    if (one1.size()>0) {
//                        return SzResult.error("车牌号已存在,请重新输入");
//                    }
                    one.setTrucksTruckno(trucksTruckno);
                    sysLogBu.logRecord("修改车辆信息", user1.getTrueName(), "修改了车牌号为" + trucksTruckno + "的车辆信息", plantId);
                    //IC卡绑定的车号也要修改
//                    QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
//                    queryWrapper3.eq("CARDINF_TRUCK_PWD", trucksTruckno1);
//                    queryWrapper3.eq("CARDINF_TYPE", "附卡(绑定车号)").or().eq("CARDINF_TYPE", "进货卡(绑定车号)");
//                    queryWrapper3.eq("CARDINF_DELETED", 0);
//                    queryWrapper3.eq("DATA_FROM", plantId);
//                    List<CIcCardsInfo> list = iCIcCardsInfoService.list(queryWrapper3);
//                    for (CIcCardsInfo cIcCardsInfo : list) {
//                        cIcCardsInfo.setCardinfTruckPwd(trucksTruckno);
//                        iCIcCardsInfoService.updateById(cIcCardsInfo);
//                    }
                }
                one.setDownedID1(0);
                iCTrucksService.updateById(one);
            } else {
                return SzResult.error("车牌不存在");
            }
        } else {
            return SzResult.error("id不能为空");
        }
        return SzResult.Success();
    }
    //修改排队vip次数
    public SzResult updateVipNum(String id,Integer vipNum){
        CTrucks truck = new CTrucks();
        truck.setId(id);
        truck.setTrucksQueueVipNums(vipNum);
        if (iCTrucksService.updateById(truck)) {
            return SzResult.success("修改成功");
        }
        return SzResult.success("修改失败");
    }
    //导出车辆表
    @Async
    public Future<List<TruckExcelEn>> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, String sheetName, String plantId,
                                                  String truckNo, Integer truckType) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<TruckExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        TruckExcelEn truckExcelEn = TruckExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("trucksTruckno")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTruckno("");
                                } else {
                                    truckExcelEn.setTrucksTruckno(entry.getValue().toString());
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("trucksTrailerno")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTrailerno("");
                                } else {
                                    truckExcelEn.setTrucksTrailerno(entry.getValue().toString());
                                }
                            }
//                                trucksType
                            if (entry.getKey().equals("trucksType")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksType("");
                                } else {
                                    if (getTrucksType(entry.getValue().toString()) != null) {
                                        truckExcelEn.setTrucksType(getTrucksType(entry.getValue().toString()).getTruckclassClass());
                                    } else {
                                        truckExcelEn.setTrucksType("");
                                    }
                                }
                            }
//                                trucksInfoApproved
                            if (entry.getKey().equals("trucksInfoApproved")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksInfoApproved("未批准");
                                } else {
                                    truckExcelEn.setTrucksInfoApproved(entry.getValue().toString());
                                }
                            }
//                                trucksPriv
                            if (entry.getKey().equals("trucksPriv")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksPriv("未知");
                                } else {
                                    truckExcelEn.setTrucksPriv("特派");
                                }
                            }
//                                trucksEmiss
                            if (entry.getKey().equals("trucksEmiss")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksEmiss("未知");
                                } else {
                                    truckExcelEn.setTrucksEmiss("国" + entry.getValue().toString());
                                }
                            }
//                                trucksAxles
                            if (entry.getKey().equals("trucksAxles")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksAxles("未知");
                                } else {
                                    truckExcelEn.setTrucksAxles(entry.getValue().toString());
                                }
                            }
//                                trucksLength
                            if (entry.getKey().equals("trucksLength")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksLength("未知");
                                } else {
                                    truckExcelEn.setTrucksLength(entry.getValue().toString());
                                }
                            }
//                                trucksMaster
                            if (entry.getKey().equals("trucksMaster")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksMaster("未知");
                                } else {
                                    truckExcelEn.setTrucksMaster(entry.getValue().toString());
                                }
                            }
//                                trucksIdentity
                            if (entry.getKey().equals("trucksIdentity")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksIdentity("未知");
                                } else {
                                    truckExcelEn.setTrucksIdentity(entry.getValue().toString());
                                }
                            }
//                                trucksTell
                            if (entry.getKey().equals("trucksTell")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksTell("未知");
                                } else {
                                    truckExcelEn.setTrucksTell(entry.getValue().toString());
                                }
                            }
//                                trucksRegMan
                            if (entry.getKey().equals("trucksRegMan")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRegMan("");
                                } else {
                                    truckExcelEn.setTrucksRegMan(entry.getValue().toString());
                                }
                            }
//                                trucksTag1
                            if (entry.getKey().equals("trucksTag1")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag1("");
                                } else {
                                    truckExcelEn.setTrucksTag1(entry.getValue().toString());
                                }
                            }
//                                trucksTag2
                            if (entry.getKey().equals("trucksTag2")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag2("");
                                } else {
                                    truckExcelEn.setTrucksTag2(entry.getValue().toString());
                                }
                            }
//                                trucksTag3
                            if (entry.getKey().equals("trucksTag3")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag3("");
                                } else {
                                    truckExcelEn.setTrucksTag3(entry.getValue().toString());
                                }
                            }
//                                trucksDispatcher
                            if (entry.getKey().equals("trucksFixMan")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksFixMan("");
                                } else {
                                    truckExcelEn.setTrucksFixMan(entry.getValue().toString());
                                }
                            }
//                                trucksDispatchTm
                            if (entry.getKey().equals("trucksFixTm")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksFixTm("");
                                } else {
                                    truckExcelEn.setTrucksFixTm(entry.getValue().toString());
                                }
                            }
//                                trucksRefTare
                            if (entry.getKey().equals("trucksRefTare")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRefTare("");
                                } else {
                                    truckExcelEn.setTrucksRefTare(entry.getValue().toString());
                                }
                            }
//                                trucksRefNet
                            if (entry.getKey().equals("trucksRefNet")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRefNet("");
                                } else {
                                    truckExcelEn.setTrucksRefNet(entry.getValue().toString());
                                }
                            }
//                                trucksAgainMinTm
                            if (entry.getKey().equals("trucksAgainMinTm")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksAgainMinTm("");
                                } else {
                                    truckExcelEn.setTrucksAgainMinTm(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(truckExcelEn);
                    }
                    break;
                case "all":
                    QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
                    queryWrapper.eq(truckType != null, "TRUCKS_TYPE", truckType);
                    queryWrapper.eq("TRUCKS_DELETED", 0);
                    queryWrapper.eq("DATA_FROM", plantId);
                    queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
                    queryWrapper.orderByDesc("ID");
                    List<CTrucks> cTrucks = iCTrucksService.list(queryWrapper);
                    for (CTrucks cTruck : cTrucks) {
                        String trucksPrivValue = "";
                        if (cTruck.getTrucksPriv() != null) {
                            trucksPrivValue = cTrucksDic.getTruckPrivDic(Integer.valueOf(String.valueOf(cTruck.getTrucksPriv())));
                        }
                        trucksPrivValue = trucksPrivValue.isEmpty() ? "0" : trucksPrivValue;
                        TruckExcelEn truckExcelEn = TruckExcelEn.builder()
                                .trucksTruckno(Optional.ofNullable(cTruck.getTrucksTruckno()).orElse(""))
                                .trucksTrailerno(Optional.ofNullable(cTruck.getTrucksTrailerno()).map(Object::toString).orElse(""))
                                //           从当前类的getTrucksType方法里获取值，如果为空，就返回空字符串
                                .trucksType(Optional.ofNullable(this.getTrucksType(String.valueOf(cTruck.getTrucksType())).getTruckclassClass()).orElse(""))
                                //                                (1.批准；0.未批准)trucksInfoApprovedDic
                                .trucksInfoApproved(Optional.ofNullable(cTrucksDic.getTrucksInfoApprovedDic(cTruck.getTrucksInfoApproved())).orElse(""))
                                .trucksPriv(trucksPrivValue)
                                .trucksEmiss(Optional.ofNullable(cTruck.getTrucksEmiss()).map(Object::toString).orElse(""))
                                .trucksAxles(Optional.ofNullable(cTruck.getTrucksAxles()).map(Object::toString).orElse(""))
                                .trucksLength(Optional.ofNullable(cTruck.getTrucksLength()).map(Object::toString).orElse(""))
                                .trucksMaster(Optional.ofNullable(cTruck.getTrucksMaster()).orElse(""))
                                .trucksIdentity(Optional.ofNullable(cTruck.getTrucksIdentity()).orElse(""))
                                .trucksTell(Optional.ofNullable(cTruck.getTrucksTell()).orElse(""))
                                .trucksRegMan(Optional.ofNullable(cTruck.getTrucksRegMan()).orElse(""))
                                .trucksTag1(Optional.ofNullable(cTruck.getTrucksTag1()).orElse(""))
                                .trucksTag2(Optional.ofNullable(cTruck.getTrucksTag2()).orElse(""))
                                .trucksTag3(Optional.ofNullable(cTruck.getTrucksTag3()).orElse(""))
                                .trucksFixMan(Optional.ofNullable(cTruck.getTrucksDispatcher()).orElse(""))
                                .trucksFixTm(Optional.ofNullable(cTruck.getTrucksDispatchTm()).map(Object::toString).orElse(""))
                                .trucksRefTare(Optional.ofNullable(cTruck.getTrucksRefTare()).map(Object::toString).orElse(""))
                                .trucksRefNet(Optional.ofNullable(cTruck.getTrucksRefNet()).map(Object::toString).orElse(""))
                                .trucksAgainMinTm(Optional.ofNullable(cTruck.getTrucksAgainMinTm()).map(Object::toString).orElse(""))
                                .build();
                        list.add(truckExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        TruckExcelEn truckExcelEn = TruckExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("trucksTruckno")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTruckno("");
                                } else {
                                    truckExcelEn.setTrucksTruckno(entry.getValue().toString());
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("trucksTrailerno")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTrailerno("");
                                } else {
                                    truckExcelEn.setTrucksTrailerno(entry.getValue().toString());
                                }
                            }
//                                trucksType
                            if (entry.getKey().equals("trucksType")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksType("");
                                } else {
                                    if (getTrucksType(entry.getValue().toString()) != null) {
                                        truckExcelEn.setTrucksType(getTrucksType(entry.getValue().toString()).getTruckclassClass());
                                    } else {
                                        truckExcelEn.setTrucksType("");
                                    }
                                }
                            }
//                                trucksInfoApproved
                            if (entry.getKey().equals("trucksInfoApproved")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksInfoApproved("未批准");
                                } else {
                                    truckExcelEn.setTrucksInfoApproved(entry.getValue().toString());
                                }
                            }
//                                trucksPriv
                            if (entry.getKey().equals("trucksPriv")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksPriv("未知");
                                } else {
                                    truckExcelEn.setTrucksPriv("特派");
                                }
                            }
//                                trucksEmiss
                            if (entry.getKey().equals("trucksEmiss")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksEmiss("未知");
                                } else {
                                    truckExcelEn.setTrucksEmiss("国" + entry.getValue().toString());
                                }
                            }
//                                trucksAxles
                            if (entry.getKey().equals("trucksAxles")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksAxles("未知");
                                } else {
                                    truckExcelEn.setTrucksAxles(entry.getValue().toString());
                                }
                            }
//                                trucksLength
                            if (entry.getKey().equals("trucksLength")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksLength("未知");
                                } else {
                                    truckExcelEn.setTrucksLength(entry.getValue().toString());
                                }
                            }
//                                trucksMaster
                            if (entry.getKey().equals("trucksMaster")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksMaster("未知");
                                } else {
                                    truckExcelEn.setTrucksMaster(entry.getValue().toString());
                                }
                            }
//                                trucksIdentity
                            if (entry.getKey().equals("trucksIdentity")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksIdentity("未知");
                                } else {
                                    truckExcelEn.setTrucksIdentity(entry.getValue().toString());
                                }
                            }
//                                trucksTell
                            if (entry.getKey().equals("trucksTell")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")
                                        || entry.getValue() == null) {
                                    truckExcelEn.setTrucksTell("未知");
                                } else {
                                    truckExcelEn.setTrucksTell(entry.getValue().toString());
                                }
                            }
//                                trucksRegMan
                            if (entry.getKey().equals("trucksRegMan")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRegMan("");
                                } else {
                                    truckExcelEn.setTrucksRegMan(entry.getValue().toString());
                                }
                            }
//                                trucksTag1
                            if (entry.getKey().equals("trucksTag1")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag1("");
                                } else {
                                    truckExcelEn.setTrucksTag1(entry.getValue().toString());
                                }
                            }
//                                trucksTag2
                            if (entry.getKey().equals("trucksTag2")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag2("");
                                } else {
                                    truckExcelEn.setTrucksTag2(entry.getValue().toString());
                                }
                            }
//                                trucksTag3
                            if (entry.getKey().equals("trucksTag3")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksTag3("");
                                } else {
                                    truckExcelEn.setTrucksTag3(entry.getValue().toString());
                                }
                            }
//                                trucksDispatcher
                            if (entry.getKey().equals("trucksFixMan")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksFixMan("");
                                } else {
                                    truckExcelEn.setTrucksFixMan(entry.getValue().toString());
                                }
                            }
//                                trucksDispatchTm
                            if (entry.getKey().equals("trucksFixTm")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksFixTm("");
                                } else {
                                    truckExcelEn.setTrucksFixTm(entry.getValue().toString());
                                }
                            }
//                                trucksRefTare
                            if (entry.getKey().equals("trucksRefTare")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRefTare("");
                                } else {
                                    truckExcelEn.setTrucksRefTare(entry.getValue().toString());
                                }
                            }
//                                trucksRefNet
                            if (entry.getKey().equals("trucksRefNet")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksRefNet("");
                                } else {
                                    truckExcelEn.setTrucksRefNet(entry.getValue().toString());
                                }
                            }
//                                trucksAgainMinTm
                            if (entry.getKey().equals("trucksAgainMinTm")) {
                                if (entry.getValue() == null || entry.getValue().toString().equals("0") || entry.getValue().toString().equals("")) {
                                    truckExcelEn.setTrucksAgainMinTm("");
                                } else {
                                    truckExcelEn.setTrucksAgainMinTm(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(truckExcelEn);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }

    //    车辆类型查询
    public CTruckClass getTrucksType(String trucksType) {
        QueryWrapper<CTruckClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKCLASS_ID", trucksType);
        queryWrapper.ne("TRUCKCLASS_DELETED", 1);
        return icTruckClassService.getOne(queryWrapper);
    }

    /**
     * 获取所有未在排队的IC卡
     */
    public SzResult getIcCardList(String token, String plantId) throws Exception {
        // String plantId = plantId;
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CARDINF_DELETED", 0);
        queryWrapper.eq("CARDINF_MODIFY_REASON", "发卡").or().eq("CARDINF_MODIFY_REASON", "解挂");
        queryWrapper.eq("CARDINF_REPORT_LOST", 0);
        queryWrapper.eq("CARDINF_CUR_VALID", 1);
        queryWrapper.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> list = iCIcCardsInfoService.list(queryWrapper);
        if (list.size() == 0) {
            return SzResult.success(list);
        } else {
            List<CIcCardsInfo> list1 = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("TRUCKS_CHECKIN_CARDNO", list.get(i).getCardinfCardno());
                queryWrapper1.between("TRUCKS_QUEUE_STATUS", 1, 6);
                queryWrapper1.eq("TRUCKS_DELETED", 0);
                queryWrapper1.eq("DATA_FROM", plantId);
                List<CTrucks> list2 = iCTrucksService.list(queryWrapper1);
                if (list2.size() == 0) {
                    list1.add(list.get(i));
                }
            }
            return SzResult.success(list1);
        }
    }


    //    查询可手动排队车辆列表
    public SzResult getQueueTruckList(String page, String pageSize, String truckNo, String bindCoId, String token, String plantId) {
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.apply(StringUtils.isNotEmpty(bindCoId),"FIND_IN_SET('" + bindCoId + "', BIND_CO_ID)");
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.eq(plantId != null && !plantId.equals(""), "DATA_FROM", plantId);
        queryWrapper.orderByDesc("ID");
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        List<TruckListVo> truckList = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TruckListVo truckListVo = new TruckListVo();
            BeanUtils.copyProperties(cTrucks,truckListVo);
            if (cTrucks.getTrucksType() != null){
                QueryWrapper<CTruckClass> truckClassQw= new QueryWrapper<>();
                truckClassQw.eq("TRUCKCLASS_ID",cTrucks.getTrucksType());
                truckListVo.setTrucksTypeName(icTruckClassService.getOne(truckClassQw).getTruckclassClass());
            }
            if (cTrucks.getBindCoId() != null && !cTrucks.getBindCoId().equals("")){
                List<Map<String,Object>> companyList = new ArrayList<>();
//                拿到车辆的所有的绑定的单位信息
                for (String s : cTrucks.getBindCoId().split(",")) {
                    CCompanys companys = iCCompanysService.getById(s);
                    Map<String, Object> map = new HashMap<>();
                    map.put("coName",companys.getCpyCo() + '.' + companys.getCpyChico());
                    map.put("id",companys.getId());
                    companyList.add(map);
                }
                truckListVo.setCompanyList(companyList);
            }
            truckList.add(truckListVo);
        }
        Map<String, Object> res = new HashMap<>();
        res.put("total", iPage.getTotal());
        res.put("list", truckList);
        return SzResult.success(res);
    }

}
